package com.team.cloudsharing.dao.actionlog;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.team.cloudsharing.dao.user.UserDO;
import com.team.cloudsharing.ultis.CastArrayList;

@Repository
public class ActionLogDAOImpl implements ActionLogDAO {

	final static Logger logger = Logger.getLogger(ActionLogDAOImpl.class);

	@Autowired
	SessionFactory sessionFactory;

	@Override
	public long insertActionLog(ActionLogDO actionLogDO) {
		long actionLogId = -1;
		Session session = null;
		if (actionLogDO != null) {
			try {
				session = sessionFactory.openSession();
				Transaction tx = session.beginTransaction();
				session.saveOrUpdate(actionLogDO);
				tx.commit();
				Serializable id = session.getIdentifier(actionLogDO);
				actionLogId = (Long) id;
			} catch (Exception e) {
				logger.error(e.getMessage());
			} finally {
				try {
					session.close();
				} catch (Exception e2) {
					logger.error(e2.getMessage());
				}
			}
		}
		return actionLogId;
	}

	@Override
	public List<ActionLogDO> setAllStatusByUserDO(UserDO userDO) {
		List<ActionLogDO> listActionLogDO = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			Criteria criteria = session.createCriteria(ActionLogDO.class);
			criteria.add(Restrictions.eq("userDO", userDO));
			listActionLogDO = CastArrayList.castCollection(criteria.list(),	ActionLogDO.class);
			
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return listActionLogDO;
	}

	@Override
	public boolean deleteActionLog(ActionLogDO actionLogDO) {
		boolean statusDelete = false;
		Session session = null;
		if (actionLogDO != null) {
			try {
				session = sessionFactory.openSession();
				Transaction tx = session.beginTransaction();
				session.delete(actionLogDO);
				tx.commit();
				statusDelete = true;
			} catch (Exception e) {
				statusDelete = false;
				logger.error(e.getMessage());
			} finally {
				try {
					session.close();
				} catch (Exception e2) {
					logger.error(e2.getMessage());
				}
			}
		}
		return statusDelete;
	}

	@Override
	public void setStatusByMacAddress(String macAddress) {
		ActionLogDO actionLogDO = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			actionLogDO = findActionLogByMacAddress(macAddress);
			actionLogDO.setStatus(false);
			insertActionLog(actionLogDO);
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
	}

	@Override
	public ActionLogDO findActionLogByMacAddress(String macAddress) {
		ActionLogDO actionLogDO = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Criteria criteria = session.createCriteria(ActionLogDO.class);
			criteria.add(Restrictions.eq("macAddress", macAddress));
			actionLogDO = (ActionLogDO) criteria.uniqueResult();
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return actionLogDO;
	}

	
}
